﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;

//this code is neater... etc...
//the bits of the ui i did need a lot of cleaning up :P
namespace KeyKeeper
{
    public partial class KKnWindow : Form
    {
        public KKnWindow()
        {
            InitializeComponent();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void populateFamilies(object sender, EventArgs e)
        {
            FileInfo[] xmlFolder = new DirectoryInfo(Directory.GetCurrentDirectory() + "\\xml").GetFiles("*.xml");
            foreach (FileInfo xmlFile in xmlFolder)
            {
                XDocument file = XDocument.Load(xmlFile.FullName);

                productSel.Items.AddRange((from Family in file.Elements("family")
                                           select new family
                                           {
                                               name = Family.Attribute("name").Value,
                                               relname = Family.Attribute("relname").Value,
                                               builds = (from Build in Family.Descendants("build")
                                                        select new build
                                                        {
                                                            number = Build.Attribute("number").Value,
                                                            keys = (from key in Build.Elements("key")
                                                                   select key.Value).ToArray(),
                                                            pass = Build.Element("pass").Value,
                                                            bios = DateTime.ParseExact(Build.Element("date").Value, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture),
                                                            prereq = Build.Element("pre").Value
                                                        }).ToArray()
                                           }).ToArray());

            }
            

        }
        private void intSel()
        {
           
        }
        private void populateBuilds(object sender, EventArgs e)
        {
            buildSel.Items.Clear();

            buildSel.Items.AddRange(((family)productSel.SelectedItem).builds.ToArray());
        }

        private void populateData(object sender, EventArgs e)
        {
            build selectedBuild = ((build)buildSel.SelectedItem);
            keyLst.Items.Clear();
            keyLst.Items.AddRange(selectedBuild.keys);
            passTxt.Text = selectedBuild.pass;
            biosTxt.Text = selectedBuild.bios.ToShortDateString();
            prereqTxt.Text = selectedBuild.prereq;
        }

        private void aBoutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About aWin = new About();
            aWin.Show();
        }

        private void buildSel_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void userBuildEditorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            userbuilds ubWin = new userbuilds();
            ubWin.Show();
        }
    }

    public class family
    {
        public string name { get; set; }
        public string relname { get; set; }

        public build[] builds { get; set; }

        public override string ToString()
        {
            return string.Format("{0} [{1}]", name, relname);
        }
    }

    public class build
    {
        public string number { get; set; }
        public string[] keys { get; set; }
        public string pass { get; set; }
        public DateTime bios { get; set; }
        public string prereq { get; set; }

        public override string ToString()
        {
            return number;
        }
    }
}
